/* Copyright by GD GameDuell GmbH, All Rights Reserved
 * This document is strictly confidential and sole property of GD GameDuell GmbH, Berlin, Germany
 */

package de.gameduell.loader.src {
	import de.gameduell.framework.application.Settings;
	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.asset.XMLAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.util.FlashVars;

	import flash.display.Sprite;
	import flash.system.Security;

	public class ExtApplicationLoader extends ApplicationLoader {
		private var external:Boolean;

		/**
		 * Setting scale mode and language according to external parameters or using defaults.
		 * Set up all loader related gfx and place them in the middle of the stage.
		 * Initializes asset loading and start loading the main config file of a application.
		 */
		public function ExtApplicationLoader() {
			Debug.trace("ApplicationLoader::Constructore");
			Security.allowDomain("*");
			super();
			external = loaderInfo.parameters["external"] == "true";
		}

		override protected function init():void {
			Debug.trace("ApplicationLoader::init");
			if( loaderInfo.parameters["external"] == "true") {
				catchVars();
				if(FlashVars.getKeyValue(FlashVars.LANGUAGE) == FlashVars.NO_VALUE) {
					FlashVars.setKeyValue(FlashVars.LANGUAGE, "en");
				}
				FlashVars.setKeyValue(FlashVars.LANGUAGE, FlashVars.getKeyValue("language").toLowerCase());
				extractUrl();
				configAsset = new XMLAsset("config", appURL + CONFIG_FILE);
				configAsset.addEventListener(AssetEvent.COMPLETE, configLoaded);
				configAsset.addEventListener(AssetEvent.LOAD_ERROR, loadError);
				configAsset.load();
				isStandalone = false;				
			} else {
				super.init();
			}
		}

		override protected function catchVars():void {
			if( loaderInfo.parameters["external"] == "true") {
				FlashVars.catchVars(loaderInfo.parameters);
				FlashVars.setKeyValue(FlashVars.LANGUAGE, loaderInfo.parameters["language"]);
				FlashVars.setKeyValue(FlashVars.XML_FOLDER, loaderInfo.parameters["xmlfolder"]);
			} else {
				super.catchVars();
			}
		}

		override protected function extractUrl():void {		
			if( loaderInfo.parameters["external"] == "true") {	
				isOnline = loaderInfo.parameters["isOnline"] == "true"; 
				appURL = loaderInfo.parameters["xmlfolder"] + "/";
				baseURL = loaderInfo.parameters["baseUrl"] + "/";
				FlashVars.setKeyValue(FlashVars.APP_URL, appURL);
			} else {
				super.extractUrl();
			}
		}

		override protected function configLoaded(event:AssetEvent):void {
			try {
				config 		 = XML(event.asset.data);
				settings 	 = new Settings(config.child(SETTINGS));
				isStandalone = settings.standalone;
				patchLanguage();
				var loaderList:XMLList = config.child(LOADER);
				_loaderAssets = new AssetGroup(LOADER, "");
				if(settings.useGeneralLangFile) {
					loaderList.appendChild(new XML(getGeneralLang()));
					
				}
				loaderList.appendChild(new XML("<lang>lang/" + settings.gameType + "_" + FlashVars.getKeyValue("language") + ".xml</lang>"));
				
				fillAssetGroup(loaderList,_loaderAssets);
				
				_loaderAssets.addEventListener(AssetEvent.COMPLETE, initLoader);
				_loaderAssets.addEventListener(AssetEvent.LOAD_ERROR, loadError);
				_loaderAssets.load();
			}catch(e:Error) {
				Debug.trace("Error loading config: " + e.getStackTrace(), Debug.ERROR);
			}
		}
		
		private function patchLanguage():void {
			var patchIt:Boolean = true;
			for(var i in settings.activeLanguages){
				if(settings.activeLanguages[i] == FlashVars.getKeyValue(FlashVars.LANGUAGE)){
					patchIt = false;
					break;
				}
			}
			if(patchIt){
				Debug.trace("ExtApplicationLoader::patchLanguage: from: " + FlashVars.getKeyValue(FlashVars.LANGUAGE) + " to: en", Debug.WARNING);
				FlashVars.setKeyValue(FlashVars.LANGUAGE, "en");
			}
		}

		override protected function initLoader(event:AssetEvent):void {		
			super.initLoader(AssetEvent(event.clone()));
		}

		override protected function startApplication():void {
			if (external) {
				var appWidth:int = int(loaderInfo.parameters["width"]);
				var appHeight:int = int(loaderInfo.parameters["height"]);
				var applicationMask:Sprite = new Sprite();
				applicationMask.graphics.beginFill(0xFFFFFF);
				applicationMask.graphics.drawRect(appWidth, 0, stage.width - appWidth, appHeight);
				applicationMask.graphics.drawRect(0, appHeight, stage.width, stage.height - appHeight);	
				stage.addChildAt(applicationMask, 0);
			}
			super.startApplication();
		}
	}
}